#include "postdata.h"
const char* host = "39.106.160.132";
String url = "http://39.106.160.132/lhlsite/laravel/public/admin/info/post_reveive";

// 通过HTTPClient类发送POST请求
void HTTPClient_POST(char tem[5]) {
    WiFiClient client;
    HTTPClient http;
    DHT dht(2, DHT11);
    dht.begin();
    
    const uint16_t port = 80;
    if (!client.connect(host, port)) {
        Serial.println("connection failed");
        delay(5000);
        return;
    } else {
        Serial.println("WiFiClient connect ok");
        if(http.begin(client, url)) {
            Serial.print("Remote Server Connect OK!");
            Serial.printf("[HTTP} connect ok\n");
    
              http.addHeader("Content-Type", "application/json; charset=UTF-8");
//            http.addHeader("api-key", APIKEY);
//            http.addHeader("Host", HOST);
//            http.addHeader("Content-Length", "10");
//            http.addHeader("Connection", "close");


            float Humidity = dht.readHumidity();
            float Temperature = dht.readTemperature();
            String postdata = "{\"" + String("PostData") +"\":" + String(Temperature)+", \"" + String("PostData2") + "\":" + String(Humidity)+"}";

            Serial.print("Message that you send-->");
            Serial.println(postdata);

            int httpCode = http.POST(postdata);

            if (httpCode > 0) {
                // 如果状态码正常
                if (httpCode == HTTP_CODE_OK) {
                    Serial.print("HTTP状态码->");
                    Serial.println(httpCode);
                    String payload = http.getString();
                    Serial.print("Message that Server return -->");
                    Serial.println(payload);

                }
            }
            else {
                Serial.println("HTTP code < 0");
                Serial.println(httpCode);
            }
            http.end();
        } else {
            Serial.printf("[HTTP} Unable to connect\n");
        }

    }

}
